<?php
namespace Auto\Controller;
use Common\Controller\AdminbaseController;

/**
 * 自动生成后台管理程序
 */
class IndexController extends AdminbaseController{

	public $model;		//模块名
	public $controller;	//控制器名称
	public $active;		//功能操作设定
	public $db_table;	//数据库
	public $index_title;	//列表标题
	public $add_title;	//新增标题
	public $file_list;	//数据列表
	public $open_control = true;	//是否生成控制器文件及目录
	public $open_tpl = true;	//是否生成模板文件及目录
	public $tpl_add;	//新增页面模板文件目录
	public $tpl_list;	//列表页模板文件
	public $tpl_control;	//控制器文件

	public function __construct()
	{
		spl_autoload_register("include_config_file");
	}

	function index(){
		exit;
		$example = new \project();
		$this->model = $example->model;
		$this->controller = $example->controller;
		$this->active = $example->active;
		$this->file_list = $example->file_list;
		$this->index_title = $example->index_title;
		$this->add_title = $example->add_title;
		$this->db_table = ucfirst($example->db_table);
		//创建文件
		$this->create_file();
		$this->write_tpl_add_data();
		$this->write_tpl_list_data();
		$this->write_control_data();
	}

	/**
	 * 获取控制器文件并写入
	 */
	function write_control_data(){
		$rep_data = array(
			'model'=>$this->model,
			'controller'=>$this->controller,
			'db_table'=>$this->db_table,
		);
		$data = get_tpl_php('control',$rep_data);
		write_file($this->tpl_control,$data);
	}

	/**
	 * 获取列表页文件内容并写入
	 */
	function write_tpl_list_data(){
		if(in_array('list',$this->active)){
			$list_table_th = "";
			$list_table_tr = "";
			foreach($this->file_list AS $fitem){
				if($fitem['list_tpl']){
					$list_table_th .= "<td>".$fitem['title']."</td>
				";
					switch($fitem['type']){
						case 'input':
							$list_table_tr .= "<td>{\$vo.".$fitem['name']."}</td>
					";
							break;
						case 'image':
							$list_table_tr .= '<td><img src="{:sp_get_image_preview_url($vo[\''.$fitem["name"].'\'])}"   width="100" /></td>
					';
							break;
						case 'create_time':
							$list_table_tr .= '<td>{:date("Y-m-d H:i:s",$vo["'.$fitem['name'].'"])}</td>
					';
							break;
						default:
							$list_table_tr .= "<td>{\$vo.".$fitem['name']."}</td>
							";
							break;
					}

				}
			}
			/**
			 * 添加操作
			 */
			$edit_list_table_tr = "";
			if(in_array('edit',$this->active)){
				$edit_list_table_tr .= '<a href="{:U(\''.$this->controller.'/add\',array(\'id\'=>\$vo[\'id\']))}">编辑</a> |';
			}
			if(in_array('delete',$this->active)){
				$edit_list_table_tr .= '<a href="{:U(\''.$this->controller.'/delete\',array(\'id\'=>\$vo[\'id\']))}">删除</a> ';
			}
			if(!empty($edit_list_table_tr)){
				$list_table_tr .= "<td>".$edit_list_table_tr."</td>";
				$list_table_th .= "<td>操作</td>";
			}

			/**
			 * 数据导入到模板
			 */
			$rep_data = array(
				'controller'=>$this->controller,
				'index_title'=>$this->index_title,
				'add_title'=>$this->add_title,
				'list_table_th'=>$list_table_th,
				'list_table_tr'=>$list_table_tr,
			);
			$content = get_tpl('index',$rep_data);
			write_file($this->tpl_list,$content);
		}
	}

	/**
	 * 获取新增文件内容并写入
	 */
	function write_tpl_add_data(){
		if(in_array('add',$this->active)){
			$rep_data = array(
				'controller'=>$this->controller,
				'index_title'=>$this->index_title,
				'add_title'=>$this->add_title
			);
			$content = get_tpl('add_header',$rep_data);

			foreach($this->file_list AS $fitem){
				if($fitem['add_tpl']){
					if(!empty($fitem['require'])){
						$fitem['requirespan'] = '<span class="form-required">*</span>';
					}else{
						$fitem['requirespan'] = "";
					}
					if($fitem['type'] == 'radio'){
						$radio_value = $fitem['value'];
						$value_content = "";
						foreach($radio_value AS $rkey => $rvalue){
							$value_content .= '<label class="radio inline" for="lightup_true"> <input type="radio"   class="radio" name="lightup" value="'.$rkey.'" />'.$rvalue.'</label> &nbsp;&nbsp;&nbsp;';
						}
						$fitem['radiolable'] = $value_content;
					}
					unset($fitem['value']);
					$content .= get_tpl('add_'.$fitem['type'],$fitem);
				}
			}
			$content .= get_tpl('add_bottom',$rep_data);
			write_file($this->tpl_add,$content);
		}


	}


	/**
	  * 生成文件
	  */
	function create_file(){
		//生成控制器相关文件
		if(!file_exists('application/'.$this->model."/Controller")){
			mkdir ('application/'.$this->model."/Controller",0777,true);
		}
		if(!file_exists('application/'.$this->model."/Controller/".$this->controller."Controller.class.php")){
			$fp=fopen('application/'.$this->model."/Controller/".$this->controller."Controller.class.php","w");
			fclose($fp);
		}
		$this->tpl_control = 'application/'.$this->model."/Controller/".$this->controller."Controller.class.php";
		//生成模板相关文件
		if($this->open_tpl && !empty($this->active)){
			$tpl_path = "admin/themes/simplebootx/";
			if(!file_exists($tpl_path.$this->model."/".$this->controller)){
				mkdir ($tpl_path.$this->model."/".$this->controller,0777,true);
			}
			$model_tpl_path = $tpl_path.$this->model."/".$this->controller;
			if(in_array('list',$this->active)){
				if(!file_exists($model_tpl_path."/index.html")){
					$fp=fopen($model_tpl_path."/index.html","w");
					fclose($fp);
				}
				$this->tpl_list = $model_tpl_path."/index.html";
			}
			if(in_array('add',$this->active) || in_array('edit',$this->active)){
				if(!file_exists($model_tpl_path."/add.html")){
					$fp=fopen($model_tpl_path."/add.html","w");
					fclose($fp);
				}
				$this->tpl_add = $model_tpl_path."/add.html";
			}
		}
		//生成数据库类库
		if(!file_exists('application/'.$this->model."/Model")){
			mkdir ('application/'.$this->model."/Model",0777,true);
		}
		if(!file_exists('application/'.$this->model."/Model/".$this->db_table."Model.class.php")){
			$fp=fopen('application/'.$this->model."/Model/".$this->db_table."Model.class.php","w");
			fclose($fp);
		}
		$data = array(
				'model'=>$this->model,
				'db_table'=>$this->db_table
		);
		$content = get_tpl_php('db_model',$data);
		write_file('application/'.$this->model."/Model/".$this->db_table."Model.class.php",$content);

	}


}